local U = require "packages.utility.utility"
local RUtil = require ("packages.aaa_Romantic.utility.rmt_util")

local skels = {}
for i = 1, 2 do

local skel = fk.CreateSkill {
  name = i == 1 and "rmt__jianwang" or "rmt__jianwangg",
  tags = {Skill.Limited},
}
skel:addEffect("active", {
  prompt = function (self, player, selected_cards, selected_targets)
    return "#rmt__jianwang:::"..((#Fk:currentRoom().alive_players + 1) // 2)
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected < ((#Fk:currentRoom().alive_players + 1) // 2) and player ~= to_select
    and not to_select:isKongcheng()
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected == ((#Fk:currentRoom().alive_players + 1) // 2)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 and not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local targets = table.simpleClone(effect.tos)
    table.insert(targets, 1, player)
    local disc = U.Discussion(player, targets, skel.name)
    if player.dead then return end
    local cards = {}
    for p, result in pairs(disc.results) do
      if not p.dead then
        local toCards = result.toCards
        if toCards then
          for _, id in ipairs(toCards) do
            if table.contains(p:getCardIds("h"), id) then
              table.insert(cards, id)
            end
          end
        end
      end
    end
    if #cards == 0 then return end
    local ex_cards = table.filter(cards, function(id) return room:getCardOwner(id) ~= player end)
    local use = room:askToUseRealCard(player, {
      pattern = cards, skill_name = skel.name, cancelable = true,
      extra_data = {expand_pile = ex_cards}
    })
    if use then
      if use.card:getColorString() == disc.color then
        local currentEvent = room.logic:getCurrentEvent()
        local lastSkill
        room.logic:getEventsByRule(GameEvent.SkillEffect, 1, function (e)
          if e.id < currentEvent.id and e.data.who == player then
            local skill = e.data.skill
            if not (skill.cardSkill or skill.global or skill.is_delay_effect) and skill:isPlayerSkill(player) then
              lastSkill = skill
              return true
            end
          end
        end, 1)
        if lastSkill then
          RUtil.resetSkill(player, lastSkill)
        end
      else
        --room:setPlayerMark(player, "@@rmt__jianwang", 1)
        room.logic:getCurrentEvent():addCleaner(function()
          room:setPlayerMark(player, "@@rmt__jianwang", 1)
        end)
      end
    end
  end,
})

skel:addEffect(fk.AfterSkillEffect, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    if player == target and player:getMark("@@rmt__jianwang") ~= 0 then
      local skill = data.skill
      return not (skill.cardSkill or skill.global or skill.is_delay_effect) and skill:isPlayerSkill(player)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@rmt__jianwang", 0)
    RUtil.resetSkill(player, data.skill)
  end,
})

Fk:loadTranslationTable{
  [skel.name] = "鉴往",
  [":"..skel.name] = "限定技，出牌阶段，你可以与半数角色议事（向上取整），然后使用其中一张议事牌，若此牌颜色与议事结果：相同，你令上次发动的技能视为未发动；不同，下次技能发动后视为未发动。",
}
table.insert(skels, skel)

end


Fk:loadTranslationTable{
  ["#rmt__jianwang"] = "鉴往:你可以与%arg名角色议事，然后使用其中一张议事牌",
  ["@@rmt__jianwang"] = "鉴往:重置下次",
  ["#rmt__jianwang-use"] = "鉴往：使用一张议事牌",

  ["$rmt__jianwang1"] = "死犹须为，况不必死，朕宁葬乡公之冢，不做陈留一王。",
  ["$rmt__jianwang2"] = "伐其魁首，赦其党羽，一如初平未竟之事。",
}

return skels
